package home.control;

import home.control.data.FactoryData;
import home.control.data.IGetData;
import home.control.logicElements.ActionTypes;
import home.control.logicElements.HomeParts;
import home.control.logicElements.InstantActionAdapter;
import home.control.service.AlarmCheck;
import home.control.service.GetData;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TooManyListenersException;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class ControlPanel extends Activity implements SeekBar.OnSeekBarChangeListener{

	SeekBar temperatureSB;
	TextView temperatureTV;
	ListView instantActionsLV;
	String TAG = "ControlPanel";
	IGetData dataConnect ;
	ToggleButton kitchenButton;
	ToggleButton bathroomButton;
	ToggleButton livingButton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d("onCreate", "STARTUJE");
		setContentView(R.layout.controlpanel);
		
		//dataConnect = FactoryData.getInstance();
		dataConnect = GetData.getInstance(); 
		
		temperatureSB = (SeekBar) findViewById(R.id.temperatureSB);
		temperatureSB.setOnSeekBarChangeListener(this);
		temperatureTV = (TextView) findViewById(R.id.temperatureTV);
	
		kitchenButton = (ToggleButton) findViewById(R.id.checkerOnKitchen);
		bathroomButton = (ToggleButton) findViewById(R.id.checkerOnBathroom);
		livingButton = (ToggleButton) findViewById(R.id.checkerOnLiving);
		
		kitchenButton.setChecked(dataConnect.checkStatus(HomeParts.KITCHEN_LIGHTS));
		bathroomButton.setChecked(dataConnect.checkStatus(HomeParts.BATHROOM_LIGHTS));
		livingButton.setChecked(dataConnect.checkStatus(HomeParts.LIVING_LIGHTS));
		
		kitchenButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					dataConnect.setStatus(HomeParts.KITCHEN_LIGHTS, isChecked);
			}
		});
		
		bathroomButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					dataConnect.setStatus(HomeParts.BATHROOM_LIGHTS, isChecked);
			}
		});
		
		livingButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
					dataConnect.setStatus(HomeParts.LIVING_LIGHTS, isChecked);
			}
		});
		
		setCurrentTemperature();
	}

	@Override
	public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
		switch(progress){
		case 0: {
			temperatureTV.setText("Off");
			break;
		}
		case 1: {
			temperatureTV.setText("18C");
			break;
		}
		case 2: {
			temperatureTV.setText("20C");
			break;
		}
		case 3: {
			temperatureTV.setText("22C");
			break;
		}
		case 4: {
			temperatureTV.setText("24C");
			break;
		}
		}
		
		
	}

	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
			// Not used
	}

	@Override
	public void onStopTrackingTouch(SeekBar seekBar) {
		Log.d(TAG, "onStopTrackingTouch");	
		int progress = seekBar.getProgress();
		switch(progress){
		case 0: {
			dataConnect.setTemperature(ActionTypes.HEATING_OFF);
			break;
		}
		case 1: {
			dataConnect.setTemperature(ActionTypes.HEATING_18);
			break;
		}
		case 2: {
			dataConnect.setTemperature(ActionTypes.HEATING_20);
			break;
		}
		case 3: {
			dataConnect.setTemperature(ActionTypes.HEATING_22);
			break;
		}
		case 4: {
			dataConnect.setTemperature(ActionTypes.HEATING_24);
			break;
		}
		}
		Toast.makeText(this, "Temperatura zmieniona", Toast.LENGTH_SHORT).show();
	}
	
	private void setCurrentTemperature() {
		ActionTypes at = dataConnect.getTemperature();
		if(at == ActionTypes.HEATING_OFF){
			temperatureSB.setProgress(0);
		}
		if(at == ActionTypes.HEATING_18){
			temperatureSB.setProgress(1);
		}
		if(at == ActionTypes.HEATING_20){
			temperatureSB.setProgress(2);
		}
		if(at == ActionTypes.HEATING_22){
			temperatureSB.setProgress(3);
		}
		if(at == ActionTypes.HEATING_24){
			temperatureSB.setProgress(4);
		}
	}
	
	@Override
	protected void onStop() {
		super.onPause();
	}
}
